package com.shopping.Controller;

import com.alibaba.fastjson.JSON;
import com.shopping.Entity.ResponseCode;
import com.shopping.Service.CommentInfoService;
import com.shopping.Service.CommentService;
import com.shopping.Service.TradeService;
import com.shopping.Entity.CommentInfo;
import com.shopping.Entity.OrderInfo;
import com.shopping.Entity.UserInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
public class ShoppingController {
    @Autowired
    CommentInfoService commentInfoService;
    @Autowired
    CommentService commentService;
    @Autowired
    TradeService tradeService;

    private ResponseCode responseCode;
    private static final String SUCCESS = "success";
    private static final String FAILED = "failed";

    /**
     * 下单功能,拆分订单列表,合并计算金额、物品数量、日期、时间等进行综合判断
     * @param userInfo
     * @param orderInfos
     * @return
     */
    @ApiOperation(value = "交易接口")
    @GetMapping(value = "/trade")
    @ApiResponses(value = {@ApiResponse(code = 0101,message = "交易成功",response = String.class),@ApiResponse(code = 0102,message = "交易失败",response = String.class)})
    @ResponseBody
    public String trade(@ApiParam("用户信息,非竞赛环境无需传入,通过session获取") @RequestBody UserInfo userInfo, @ApiParam("订单信息,组合订单合并成list传入") @RequestBody List<OrderInfo> orderInfos){
        boolean b = tradeService.goodsBillCount(orderInfos);
        if (b){
            responseCode = new ResponseCode(SUCCESS,"交易成功!");
        }else {
            responseCode = new ResponseCode(FAILED, "交易失败!");
        }
        return JSON.toJSONString(responseCode);
    }

    /**
     * 评论功能，根据评论订单信息自动判断是否为补充评论
     * @param userInfo
     * @param commentInfo
     * @return
     */
    @ApiOperation(value = "评论接口")
    @GetMapping(value = "/comment")
    @ApiResponses(value = {@ApiResponse(code = 0201,message = "评论完成",response = String.class),@ApiResponse(code = 0202,message = "已更新评论", response = String.class)})
    @ResponseBody
    public String comment(@ApiParam("用户信息,非竞赛环境无需传入,通过session获取") @RequestBody UserInfo userInfo,@ApiParam("评论信息") @RequestBody CommentInfo commentInfo){
        boolean flag = commentService.commentSubmit(commentInfo,userInfo);
        if (flag){
            responseCode = new ResponseCode(SUCCESS,"评论完成");
        }else {
            responseCode = new ResponseCode(SUCCESS,"已更新评论");
        }
        return JSON.toJSONString(responseCode);
    }
}